package com.cma.typhoon.web.controller;

import com.cma.model.db.Histcbasic;
import com.cma.model.db.Histctrack_Tcname;
import com.cma.model.db.SysRole;
import com.cma.model.db.SysUser;
import com.cma.model.param.HistctrackParam;
import com.cma.model.resultdb.SimpleTf;
import com.cma.model.sso.LoginRequired;
import com.cma.model.sso.LoginType;
import com.cma.service.*;
import com.cma.util.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by baijs on 2016/12/27.
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    private TwUserService twUserService;
    @Autowired
    private HistcbasicService histcbasicService;
    @Autowired
    private HistctrackService histctrackService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;

//    @RequiresPermissions("user:view")
    @RequestMapping({"/","index"})
    public String index(Model model, HttpServletRequest request){
        try{
            Subject sub= SecurityUtils.getSubject();
            String user=(String)sub.getSession().getAttribute("user");

            model.addAttribute("user",user);
            if(!StringUtils.isEmpty(user)){
                List<SysRole> list=sysRoleService.findRoleByUserName(user);//获取角色列表
                String roleId="";
                if(list!=null &&list.size()>0){
                    for(SysRole sysRole:list){
                        if(roleId.length()>0){
                            roleId=roleId+","+sysRole.getId();
                        }else{
                            roleId=sysRole.getId()+"";
                        }
                    }
                }
                if(roleId.length()>0){
                    // TODO 权限字符串 格式为: 权限名称:权限描述 每一个权限以逗号隔开
                    String permission= sysUserService.getPermissionsByRoleIds(roleId);
                    model.addAttribute("permission",permission);
                }
            }
           String year= DateUtils.getYear();
            int years=Integer.valueOf(year);
            List<Integer> yearsList=new ArrayList<>();
            for(int i=(years-1);i>=1949;i--){
                yearsList.add(i);
            }
            HistctrackParam hp=new HistctrackParam();
            String years1=(years-1)+"";
            hp.setYear(years1);
            List<SimpleTf> list= histctrackService.queryTfByParams(hp);
            model.addAttribute("tflist",list);
            model.addAttribute("years",yearsList);
        }catch (Exception e){
            e.printStackTrace();
        }
//        TwUser user=twUserService.findById(1L);
        return "map/index";
    }

    @RequestMapping("gaode.html")
    public String gaode(Model model){
        return "map/indexGaoDe";
    }

    @RequestMapping("geoq.html")
    public String geoq(Model model){
        return "map/indexGeoq";
    }

    @RequestMapping("tianditu.html")
    public String tianditu(Model model){
        return "map/indexTianDiTu";
    }

    @RequestMapping("google.html")
    public String google(Model model){
        return "map/indexGoogle";
    }

    @RequestMapping("test")
    @ResponseBody
    public Object test(){
        List<Histcbasic> his= histcbasicService.test1(1);
        return his;
    }
    @RequestMapping("test2")
    @ResponseBody
    public Object test2(){
        List<Histctrack_Tcname> his= histctrackService.test1(1);
        return his;
    }

}
